package leetcode;

import java.util.HashMap;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0350.java
 * @Author : WangRuoyu
 * @Date 2023/11/8 11:15
 */


public class Q0350 {
    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        HashMap<Integer, Integer> map2 = new HashMap<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        int cnt = 0;
        for (int i = 0; i < nums1.length; i++) {
            if (!map1.containsKey(nums1[i])) {
                map1.put(nums1[i], 1);
            } else {
                map1.put(nums1[i], map1.get(nums1[i]) + 1);
            }
        }
        for (int i = 0; i < nums2.length; i++) {
            if (!map2.containsKey(nums2[i])) {
                map2.put(nums2[i], 1);
            } else {
                map2.put(nums2[i], map2.get(nums2[i]) + 1);
            }
        }
        for (Integer i : map1.keySet()) {
            if (map2.containsKey(i)) {
                map.put(i, Math.min(map1.get(i), map2.get(i)));
                cnt += map.get(i);
            }
        }
        int[] res = new int[cnt];
        int k = 0;
        for (Integer i : map.keySet()) {
            for (int j = 0; j < map.get(i); j++) {
                res[k++] = i;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Q0350 sl = new Q0350();
    }
}